<?php

class Third_Navigation extends Zend_Navigation{
	
	protected $configs;
	protected $nivel;

	function __construct( $nivel, $pages = array() ){
		parent::__construct($pages);

		$this->configs = Zend_Registry::get('configs');

		$this->setNivel( $nivel );
		$this->setPages( $this->getMenu() );
	//	$this->('id','navigation');
	}

	/**
	 * @param $nivel the $nivel to set
	 */
	public function setNivel($nivel) {
		$this->nivel = $nivel;
	}

	/**
	 * @return the $nivel
	 */
	public function getNivel() {
		return $this->nivel;
	}

	
	function getMenu(){
		$nivel = $this->getNivel();
		$pages = array();

		if( $nivel ){
			$menuByNivel = Zend_Registry::get( 'privileges' );

			// Zend_Debug::dump( $menuByNivel );

			$menuBase = ( $this->configs['third']['menu'] );
			$i = 0;
			foreach( $menuBase as $menu ){
				$menu = htmlentities( $menu );
				$label = $menu;

				$pages[$i] = array(
	                'label' => $label,
					'module'=>'third',
	                'controller' => 'dashboard',
					'action'=>'index',
	                'title' => 'Modulos de '.$label,
	                'active' => false
	            );
	            
	            //Zend_Debug::dump($pages);
	            
	            $pageContainers = array();
	            $j=0;
	            foreach( $menuByNivel as $page ) {
	            	if( isset( $page['menu_secao'] ) ){
	            		
	            		if( $page['menu_secao'] == $menu ){
		            		$label = $page['label'] ? $page['label'] : $page['descricao'];

		            		$label = htmlentities( $label );
		            		
		            		$pageContainers[$j] = array(
				                'label' => $label,
		            			'module' => $page['module'],
		            			'controller' => $page['controller'],
				                'action' => $page['action'],
				                'title' => $page['descricao'],
				                'active' => false
				            );

				            // SUBPAGES
				            $subPages = array();
				            foreach( $menuByNivel as $subpage ){
				            	$subPageLabel = $subpage['label'] ? $subpage['label'] : $subpage['descricao'];
				            	$subPageLabel = htmlentities( $subPageLabel );

				            	if(( $page['controller'] == $subpage['controller'] ) && 
				            		( $subpage['menu'] == 1 ) &&
				            		( $subpage['action'] ) ){

				            		$subPages[] = array(
							                'label' => $subPageLabel,
					            			'module' => $subpage['module'],
					            			'controller' => $subpage['controller'],
							                'action' => $subpage['action'],
							                'title' => $subpage['descricao'],
							                'active' => false
							        );
				            	}
				            }
							
				            $pageContainers[$j]['pages'] = $subPages;
		            	}
	            	}
	            	$j++;
	            }
	            $pages[$i]['pages'] = $pageContainers;
	            $i++;
			}
			//Zend_Debug::dump( $pages  ) ;
		}
		
		return $pages;
	}
}

?>